package com.hc.server.util;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Vector;

public class HttpConnect {

	private HttpURLConnection hconn = null;
	private OutputStream os = null;
	private InputStream is = null;

	/**
	 *
	 * @param connectUrl
	 * @throws MalformedURLException
	 * @throws IOException
	 */
	public HttpConnect(String connectUrl) throws MalformedURLException,
			IOException {
		hconn = (HttpURLConnection) new URL(connectUrl).openConnection();
		hconn.setRequestMethod("POST");
		hconn.setDoInput(true);
		hconn.setDoOutput(true);
		hconn.setRequestProperty("Content-Type", "application/json");
	}

	public HttpConnect(String connectUrl, String MIME_TYPE) throws MalformedURLException,
		IOException {
		hconn = (HttpURLConnection) new URL(connectUrl).openConnection();
		hconn.setRequestMethod("POST");
		hconn.setDoInput(true);
		hconn.setDoOutput(true);
		hconn.setRequestProperty("Content-Type", MIME_TYPE);
	}
	/**
	 *
	 * @param connectUrl
	 * @param method
	 * @throws MalformedURLException
	 * @throws IOException
	 */
//	public HttpConnect(String connectUrl,String method) throws MalformedURLException,
//			IOException {
//		hconn = (HttpURLConnection) new URL(connectUrl).openConnection();
//		hconn.setRequestMethod(method);
//		hconn.setDoInput(true);
//		hconn.setDoOutput(true);
//	}

	/**
	 * 设置头信息
	 * @param key
	 * @param value
	 */
	public void setHeader(String key, String value) {
		hconn.setRequestProperty(key, value);
	}

	/**
	 * 发送数据
	 * @param data
	 * @throws IOException
	 */
	public void SendData(String data) throws IOException {
		byte[] f = data.getBytes("UTF-8");
		os = hconn.getOutputStream();
		os.write(f, 0, f.length);
		os.flush();
		os.close();
	}

	/**
	 *
	 * @return
	 * @throws IOException
	 */
	public String ReceiveData() throws IOException {
		is = new DataInputStream(hconn.getInputStream());
		StringBuffer reMsg = new StringBuffer();
		int ch;
		while ((ch = is.read()) != -1) {
			reMsg.append((char) ch);
		}
		String ret = reMsg.toString();
		return ret;
	}

	/**
	 * 释放连接
	 */
	public void releaseHttpConnect() {
		try {
			if (hconn != null) {
				hconn.disconnect();
			}
			if (os != null) {
				os.close();
			}
			if (is != null) {
				is.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 将UTF-8byte转换成String
	 * @param rec
	 * @return
	 */
	public String BytesToString(byte[] rec) {
		ByteArrayInputStream bais = new ByteArrayInputStream(rec);
		DataInputStream dis = new DataInputStream(bais);
		String BTS = null;
		try {
			BTS = new String(rec, "UTF-8");
			bais.close();
			dis.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return BTS;
	}

	/**
	 * 数据流拷贝
	 * @param in
	 * @param out
	 * @throws IOException
	 */
	private void copy(InputStream in, OutputStream out) throws IOException {
		try {
			byte[] buffer = new byte[4096];
			int nrOfBytes = -1;
			while ((nrOfBytes = in.read(buffer)) != -1) {
				out.write(buffer, 0, nrOfBytes);
			}
			out.flush();
		} catch (IOException e) {

		} finally {
			try {
				if (in != null) {
					in.close();
				}
			} catch (IOException ex) {

			}
			try {
				if (out != null) {
					out.close();
				}
			} catch (IOException ex) {

			}
		}
	}

	/**
	 * 读取文件数据
	 * @param fileName
	 * @return
	 */
	public byte[] getFileByte(String fileName) {
		InputStream fileInputStream = getClass().getResourceAsStream(fileName);
		return getFileByte(fileInputStream);
	}

	/**
	 * 读取文件数据
	 * @param fileName
	 * @return
	 */
	public String getFileString(String fileName) {
		InputStream fileInputStream = getClass().getResourceAsStream(fileName);
		return getFileString(fileInputStream);
	}

	/**
	 * 读取文件数据
	 * @param in
	 * @return
	 */
	public String getFileString(InputStream in) {

		ByteArrayOutputStream out = new ByteArrayOutputStream(4096);
		try {
			copy(in, out);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return out.toString();
	}

	/**
	 * 读取文件数据
	 * @param in
	 * @return
	 */
	public byte[] getFileByte(InputStream in) {
		ByteArrayOutputStream out = new ByteArrayOutputStream(4096);
		try {
			copy(in, out);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return out.toByteArray();
	}

	/**
	 * 读取UTF数据
	 * @return
	 * @throws IOException
	 */
	public String ReceiveUTFString() throws IOException {
		is = new DataInputStream(hconn.getInputStream());
		Vector<byte[]> bufLst = new Vector<byte[]>();

		int len = 0;
		while (true) {
			byte[] buf = new byte[256];
			int l = is.read(buf, 1, 255);
			if (l == -1) {
				break;
			}
			buf[0] = (byte) ((byte) l & 0xFF);
			len += (int) buf[0] & 0xFF;
			bufLst.add(buf);
		}
		byte[] bigBuf = new byte[len];
		int off = 0;
		for (int i = 0; i < bufLst.size(); i++) {
			System.arraycopy(bufLst.get(i), 1, bigBuf, off,
					(int) bufLst.get(i)[0] & 0xFF);
			off += (int) bufLst.get(i)[0] & 0xFF;
		}
		String ret = new String(bigBuf, "UTF-8");
		return ret;
	}
	/**
	 * 根据编码获取数据
	 * @param encoding
	 * @return
	 */
	public String receiveByEncode(String encoding) {
		DataInputStream in;
		try {
			in = new DataInputStream(hconn.getInputStream());
			InputStreamReader inread = new InputStreamReader(in, encoding);
			StringBuffer reMsg = new StringBuffer();
			int ch;
			while ((ch = inread.read()) != -1) {
				reMsg.append((char) ch);
			}
			String ret = reMsg.toString();
			return ret;
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}

	}

}
